


********************************************
*
*		Figure 2 Code
*
********************************************

clear all
cd "/Users/michaelflynn/Dropbox/EmigrationSafetyValve/JPR Submission/JPR Data & Do Files"
use "/Users/michaelflynn/Dropbox/EmigrationSafetyValve/JPR Submission/JPR Data & Do Files/ESV-MasterData(1980-2009).dta"
xtset cowcode year
set more off 
 set seed 36984 

estsimp nbreg totalprotest unemploy_g7 formov g7formov kof_eg  kofegXg7unemp dommov physint polityb ln_develop growth ln_population adultpop war colonydum  peaceyr_tp if G7==0 & rights<2&democracy==0, robust
preserve

setx mean //first set all to means, then modifiy where needed (below)
setx war 0
setx colonydum 0
 
local a = 6 /* Unemployment */
local b = 0 /* Foreign Movement */
local c = `a'*`b' /* Unemployment * Foreign Movement */
local d = 40 /* KOF Economic Globalization */
local e = `a' * `d' /* Unemployment * KOF Economic Globalization */

setx unemploy_g7 (`a') formov (`b') g7formov (`c') kof_eg (`d') kofegXg7unemp (`e') /*opening prediction */

simqi, fd(ev) changex(formov `b' 2 g7formov `c' `a'*2 kofegXg7unemp `e' `d'*`a') msims(10000)
macro list
scalar list 

postutil clear
postfile mypost probY upper lower using simresults, replace 
noisily display "start"
*set obs 10000 
while `a' <= 8.8 /* Maximum Unemployment */ {

qui simqi, fd(ev) changex(formov `b' 2 g7formov `c' `a'*2 kofegXg7unemp `e' `d'*`a') msims(10000)
scalar probY= Pr
scalar upper = PrU
scalar lower = PrL
post mypost (probY) (upper) (lower)
scalar drop probY upper lower
local a = `a'+.1 	/* Increasing unemployment by 0.1 each iteration */
local b = `b' 		/* Foreign movement stays the same */
local c = `a'*`b'	/* Reset the base interaction */
local d = 40		/* Setting base KOF Economic Globalization value */
local e = `a'*`d'	/* Setting the KOF and unemployment interaction */
setx unemploy_g7 (`a') formov (`b') g7formov (`c') kof_eg (`d') kofegXg7unemp (`e') 
display "." _c 
}
display ""

postclose mypost 

use simresults, clear 

sum
range unemploy_g7 6 8.8 29
gsort probY upper lower -unemploy_g7 


version 12
graph twoway line probY unemploy_g7, clwidth(medium) clcolor(black) clpattern(solid) ///
		|| line upper unemploy_g7, lpattern(dash) ///
		|| line lower unemploy_g7, lpattern(dash) ///
		yline(0, lwidth(medthick)) ///
         fi(100) legend(off) ///
        xlabel(6(.2)8.8) xtitle(Average G7 unemployment (%)) ytitle(Marginal effect)

graph export "/Users/michaelflynn/Dropbox/EmigrationSafetyValve/JPR Submission/JPR Final Files/Figure 2.pdf", replace





********************************************
*
*		Figure 3 Code
*
********************************************

clear all
cd "/Users/michaelflynn/Dropbox/EmigrationSafetyValve/JPR Submission/JPR Data & Do Files"
use "/Users/michaelflynn/Dropbox/EmigrationSafetyValve/JPR Submission/JPR Data & Do Files/ESV-MasterData(1980-2009).dta"
xtset cowcode year
set more off 
set seed 5269
 
*Predict Interaction Graph - Figure 2

nbreg totalprotest unemploy_g7 formov g7formov kof_eg  kofegXg7unemp dommov physint polityb ln_develop growth ln_population adultpop war colonydum  peaceyr_tp if G7==0 & rights<2&democracy==0, robust
sum unemploy_g7 formov g7formov kof_eg kofegXg7unemp dommov physint polityb ln_develop growth ln_population adultpop war colonydum  peaceyr_tp if e(sample)
clear

range unemploy_g7 6 8.8 100
gen formov=0
gen g7formov=formov*unemploy_g7
gen kof_eg=42.97519
gen kofegXg7unemp=kof_eg*unemploy_g7
gen dommov=1
gen physint=3.721443
gen polityb=-5.791583
gen ln_develop=7.887418
gen growth=4.356609
gen ln_population=16.54921
gen adultpop=57.83667
gen war=0
gen colonydum=1
gen peaceyr_tp=4.314629

***Predict will generate counts, but since you have to have xb to use the link function to convert the standard error of xb into a confidence interval, I just predict xb and convert all of them to counts
predict xb0, xb
predict stdp0, stdp
replace formov=1
replace g7formov=formov*unemploy_g7
predict xb1, xb
predict stdp1, stdp
replace formov=2
replace g7formov=formov*unemploy_g7
predict xb2, xb
predict stdp2, stdp

***Generating a 90% confidence interval around xb
gen xbci0=stdp0*1.645
gen xbci1=stdp1*1.645
gen xbci2=stdp2*1.645
gen loxb0=xb0-xbci0
gen hixb0=xb0+xbci0
gen loxb1=xb1-xbci1
gen hixb1=xb1+xbci1
gen loxb2=xb2-xbci2
gen hixb2=xb2+xbci2

***Converting all to counts
gen count0=exp(xb0)
gen lo0=exp(loxb0)
gen hi0=exp(hixb0)
gen count1=exp(xb1)
gen lo1=exp(loxb1)
gen hi1=exp(hixb1)
gen count2=exp(xb2)
gen lo2=exp(loxb2)
gen hi2=exp(hixb2)

***Generate Predicted Counts Graph
graph twoway line count0 count1 count2 unemploy_g7

***Probability > 0
*First, calculate predicted probabilities for = 0
replace formov=0
replace g7formov=formov*unemploy_g7
predict prob00, pr(0)
replace formov=1
replace g7formov=formov*unemploy_g7
predict prob01, pr(0)
replace formov=2
replace g7formov=formov*unemploy_g7
predict prob02, pr(0)

*Generate inverse
gen probgreat00=1-prob00
gen probgreat01=1-prob01
gen probgreat02=1-prob02

*Generate Predicted Probabilities Graph
graph twoway line probgreat00 unemploy_g7, lpattern(shortdash) ///
|| line probgreat01 unemploy_g7, lpattern(dash) ///
|| line probgreat02 unemploy_g7, lpattern(solid) /// 
xtitle(Average G7 unemployment (%)) ytitle(Predicted probability) ///
legend(title(Foreign movement,  size(3.5)) lab(1 "0") lab(2 "1") lab(3 "2") size(3) symxsize(5) pos(3) ) xlabel(6(.2)8.8, labsize(3.5))

graph export "/Users/michaelflynn/Dropbox/EmigrationSafetyValve/JPR Submission/JPR Final Files/Figure 3.pdf", replace


